<template>
    <button class="loginBtn" 
		@getphonenumber='getPhoneNumber' 
		open-type="getPhoneNumber" 
		type="default" 
		:style="{
			borderRadius:btnRadius,
			background:btnBj,
			height:btnHeight,
			color:fontColor
		}"
		@click="clickBtn">
    	{{btnTitle}}
		<image :src="butImg" v-if="butImg!=''"></image>
    </button>
</template>

<script>
    export default {
        name: 'authorLogin',
        props: {
            //标题
            title: {
            	type: String,
            	default: function() {
            		return '';
            	}
            },
			// 高度，单位任意
			height: {
				type: [String, Number],
				default: 'auto'
			},
			// 圆角，单位任意
			borderRadius: {
				type: [String, Number],
				default: 0
			},
			//导航背景色，支持渐变
			bgColor: {
				type: [String, Array],
				default: function() {
					return '#FFFFFF';
				}
			},
			// 字体颜色
			fontColor:{
				type: [String, Array],
				default: function() {
					return '#FFFFFF';
				}
			},
			// button里面的图片
			butImg:{
				type: String,
				default: function() {
					return '';
				}
			}
        },
        data() {
            return {
				code:'',
				//标题
				btnTitle: '',
				// 高
				btnHeight:'',
				// 背景
				btnBj:'',
				// 圆角
				btnRadius:0,
				// 图片
				btnImg:'',
            };
        },
		mounted: function(option) {
			var _this = this;
			uni.login({
				success: res => {
					_this.code = res.code;
				},
				fail: () => {
					uni.showModal({
						content:"没有获取到登陆信息，请重试"
					})
				}
			});
		},
		watch:{
			title(val) {
				this.btnTitle = val;
			},
			height(val){
				this.btnHeight = val;
			},
			borderRadius(val){
				this.btnRadius = val;
			},
			bgColor(val){
				this.btnBj = val;
			}
		},
		created(){
			this.btnTitle = this.title;
			this.btnHeight = this.height;
			this.btnRadius = this.borderRadius;
			this.btnBj = this.bgColor;
		},
        methods: {
			clickBtn(){
				this.$emit('clickAgain');
			},
            getPhoneNumber(e) {
				let _this = this;
				let ret=e.detail;
				this.testToken('/fun/member/is-bind-phone',{}).then(res => {
					if(res.data.status==200){//但token无效
						if(res.data.data.is_bind==0){
							_this.apipost('/wxxcx/get-user-login',{code: _this.code}).then(res =>{
								if(res.data.status == 200) {
									uni.setStorage({
										key:'TOKEN',
										data:res.data.data.token,
										success: function () {
											if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
												console.log('用户拒绝提供手机号');  
											} else {
												_this.savePhone(e.detail)
											}
										}
									})
								}
							})
						}
					}
				})
            },
			savePhone(info){
				var _this = this;
				let infoVal = {
					encryptedData:info.encryptedData,
					iv:info.iv,
					from:1,
				}
				this.apipost('/wxxcx/update-phone',infoVal).then(res =>{
					if(res.data.status == 200) {
						uni.showToast({
							title: '手机绑定成功',
							icon: 'success',
							duration: 2000,
							success: function() {
								_this.$emit("isBindChange",1);
							}
						})
					}
				}) 
			}
        }
    }
</script>

<style lang="scss">
.loginBtn{
	font-size: 32rpx;
}
.loginBtn::after{ border: none; }
</style>
